package com.jy.rhin.infra.repo.notfication.receive;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.jy.common.UIDWorker;
import com.jy.rhin.domain.notification.receive.ReceiveMessageDao;
import com.jy.rhin.domain.notification.receive.entity.ReceiveMessageDO;
import com.jy.rhin.infra.repo.notfication.receive.mapper.ReceiveMessageMapper;
import com.jy.rhin.infra.repo.notfication.receive.po.ReceiveMessagePO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author dhl
 * @date 2022/6/28
 * @Description
 */
@Service
public class ReceiveMessageDaoImpl implements ReceiveMessageDao {

    @Resource
    private ReceiveMessageMapper receiveMessageMapper;
    @Resource
    UIDWorker uidWorker;

    @Override
    public Long receive(ReceiveMessageDO receive){
        ReceiveMessagePO po = new ReceiveMessagePO(uidWorker.nextId(), receive);
        receiveMessageMapper.insert(po);
        return po.getId();
    }

    @Override
    public ReceiveMessageDO queryById(Long id) {
        ReceiveMessagePO po = receiveMessageMapper.selectById(id);
        return ObjectUtils.isNull(po) ? null : po.toDO();
    }


    @Override
    public ReceiveMessageDO queryByMsgId(String msgId) {
        QueryWrapper<ReceiveMessagePO> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(ReceiveMessagePO::getMsgId, msgId);
        ReceiveMessagePO po = receiveMessageMapper.selectOne(queryWrapper);
        return ObjectUtils.isNull(po) ? null : po.toDO();
    }

}
